import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/business/home/controller/add_moto_controller.dart';
import 'package:dmz/business/home/widgets/sao_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:gif_view/gif_view.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

class AddBickePage extends BaseStatefulWidget<AddBikeController> {
  const AddBickePage({super.key});

  @override
  bool showTitleBar() => true;

  @override
  String titleString() => controller.pageType == 0 ? '绑定爱车'.tr : '扫码绑定'.tr;

  @override
  List<Widget>? appBarActionWidget(BuildContext context) {
    return [
      DefaultText(controller.pageType == 0 ? '常见问题'.tr : '相册导入'.tr, 28.sp,
              textColor: Colors.white)
          .paddingOnly(right: 32.sp)
          .addGestureDetector(controller.navigationTopAction)
    ];
  }

  @override
  Widget buildContent(BuildContext context) {
    controller.context = context;
    return Stack(alignment: Alignment.bottomCenter, children: [
      Stack(alignment: Alignment.center, children: [
        PageView(
            controller: controller.pageController,
            physics: const NeverScrollableScrollPhysics(),
            children: [
              radarScanWidget(controller.gifController),
              scanCodeWidget()
            ]),
        Positioned(
            bottom: 112.sp,
            child: bottomWidget(controller.pageType, controller.changePage,
                controller.blueStarScan)),
      ]),
    ]);
  }

  ///二维码扫描
  scanCodeWidget() {
    return Stack(alignment: Alignment.center, children: [
      Container(
          child: QRView(
              key: controller.qrKey,
              onQRViewCreated: controller.onQRViewCreated)),
      Positioned(top: 300.sp, child: const SaoWidget()
          // Container(
          //     color: Colors.transparent,
          //     width: 500.sp,
          //     height: 500.sp,
          //     child: Image.asset('assets/gif/imgToGif-4.gif'))
          )
    ]);
  }

  ///雷达扫描
  radarScanWidget(controller) {
    // Image.asset('assets/gif/radar.gif', width: 500.sp, height: 500.sp)
    return Container(
        alignment: Alignment.topCenter,
        padding: EdgeInsets.only(top: 100.sp),
        color: Colors.black,
        child: GifView.asset(
          'assets/gif/radar.gif',
          width: 500.sp,
          height: 500.sp,
          controller: controller,
        ));
  }

  ///底部按钮
  bottomWidget(int selectedIndex, Function(int clickType) changePage,
      Function() startScan) {
    Widget clickItem(int type, bool selected) {
      String getImageName() {
        return type == 0
            ? 'assets/images/home/scan/blue_scan_${selected ? 'open' : 'close'}.png'
            : 'assets/images/home/scan/code_scan_${selected ? 'open' : 'close'}.png';
      }

      return Container(
              decoration: BoxDecoration(
                color: selected ? Color(0xA6000000) : Colors.transparent,
                borderRadius: BorderRadius.circular(60.sp),
              ),
              width: 124.sp,
              height: 60.sp,
              child: Image.asset(getImageName()))
          .addGestureDetector(() => changePage(type));
    }

    return Column(children: [
      Row(
        children: [
          DefaultText('蓝牙绑定'.tr, 24.sp, textColor: Color(0xFFF9F9F9)),
          SizedBox(width: 50.sp),
          DefaultText('扫一扫'.tr, 24.sp, textColor: Color(0xFFF9F9F9))
        ],
      ).marginOnly(bottom: 12.sp),
      Container(
          padding: EdgeInsets.only(left: 10.sp, right: 10.sp),
          height: 80.sp,
          decoration: BoxDecoration(
            color: const Color(0xFF515257),
            borderRadius: BorderRadius.circular(80.sp),
          ),
          child: Row(children: [
            clickItem(0, selectedIndex == 0),
            clickItem(1, selectedIndex != 0)
          ])),
      selectedIndex == 0
          ? DefaultBtn(click: controller.blueStarScan, title: '开始扫描'.tr)
              .marginOnly(top: 54.sp)
          : SizedBox(height: 120.sp + 54.sp)
    ]);
  }
}
